<?php
/**
 * 语音短信接口类
 */
class CT_sms_voice
{
    public function __construct()
    {
        $this->ci = &get_instance();
        $this->ci->config->load('sms', TRUE);
        $this->cfg = $this->ci->config->item('sms');
        $this->mobile = null;
        $this->msg = null;
        $this->gateway = null;
        $this->code = null;
    }

    /**
     * 手机号验证
     *
     * @param  string $mobile
     * @return boolean
     */
    public function set_mobile($mobile) {
        if (preg_match('/^(13|15|18|17)[0-9]{9}$/i', $mobile)) {
            $this->mobile = $mobile;
            return true;
        }
        
        return false;
    }
    
    /**
     * 生成短信内容
     * @param string $type 默认是发送验证码短信
     * @param string $msg
     * @return mixed|string
     */
    public function set_get_msg($type='valicode', $msg='')
    {
        $content = $msg;
        //如果检测到 使用的是配置文件中的短信模板 那么执行一些替换工作
        if ($type && isset($this->cfg['template'][$type]))
        {
            $content = $this->cfg['template'][$type];
            $content = $this->replace($content);
        }
    
        return $this->msg = $content;
    }
    
    /**
     * 短信发送接口
     * @return int
     */
    public function send()
    {
        $this->get_gateway();
    
        /*
        {
            "reason": "语音验证码发送成功，请注意接听",
            "result": {
                "valicode": "12345678", //验证码内容
                "callSid": "1408071238531349000500010002409e", //验证码标识
                "dateCreated": "2014-08-07 12:38:53" //发送时间
            },
            "error_code": 0 //返回
        }
        */
        
        $res = request_url($this->gateway);
        $result = json_decode($res, TRUE);
    
        if($result['error_code'] == 0)
        {
            return true;
        }
    
        return false;
    }
    
    /**
     * 生成随机码
     * @return int|null
     */
    private function set_code()
    {
        $this->code = rand(1000, 9999);
        
        return $this->code;
    }
    
    /**
     * 获取随机码
     * @return int|null
     */
    public function get_code()
    {
        return $this->code;
    }
   
    /**
     * 短信符号替换
     * 暂时就一个验证码，后续如果需要可以继续添加短信符号
     * @param $content
     * @return mixed
     */
    private function replace($content)
    {
        $arr = array(
            '{CODE}' => $this->set_code(),
        );
        //这样的写法是 兼容 多种短信模板的情况
        $content = str_replace(array_keys($arr), array_values($arr), $content);
        
        return $content;
    }

    /**
     * 获取短信网关
     * @return mixed|null|string
     */
    private function get_gateway()
    {
        //直接读取全国的短信接口网关
        $gateway_cfg = $this->cfg['gateway'];
        $gateway = $gateway_cfg[2];

        $this->gateway = str_replace(
            array('{MOBILE}', '{TPLVALUE}'), 
            array($this->mobile, $this->msg), 
            $gateway 
        );
    }
    
}
